/*   **********************************************************************  **
 **   Copyright notice                                                       **
 **                                                                          **
 **   (c) 2009, Around(J2)ME				                                 **
 **   All rights reserved.                                                   **
 **                                                                          **
 **   This program and the accompanying materials are made available under   **
 **   the terms of the New BSD License which accompanies this    			 **
 **   distribution.											                 **
 **   A copy is found in the textfile LICENSE.txt							 **
 **                                                                          **
 **   This copyright notice MUST APPEAR in all copies of the file!           **
 **                                                                          **
 **   Main developers:                                                       **
 **     Juri Strumpflohner		http://blog.js-development.com	             **
 **		Matthias Braunhofer		http://matthias.jimdo.com                    **
 **                                                                          **
 **  **********************************************************************  */

package com.aroundj2me.uicomponents;

import javax.microedition.lcdui.CustomItem;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
/**
 * UI component representing an ImageButton
 *
 */
public class ImageButton extends CustomItem {
	private Image img;
	private String text;
	private Font font;
	private final int BORDER_OFFSET = 5;
	private final int SPACE = 5;
	
	public ImageButton(String text, Image img) {
		super(null);
		this.text = text;
		this.img = img;
		font = Font.getDefaultFont();
	}

	protected int getMinContentHeight() {
		int height = 0;
		if(font.getHeight()>img.getHeight())
			height = font.getHeight();
		else
			height = img.getHeight();
		return height + BORDER_OFFSET;
	}

	protected int getMinContentWidth() {
		return img.getWidth() + font.stringWidth(text) + SPACE*2 + BORDER_OFFSET;
	}

	protected int getPrefContentHeight(int arg0) {
		return getMinContentHeight();
	}

	protected int getPrefContentWidth(int arg0) {
		return getMinContentWidth();
	}

	protected void paint(Graphics g, int w, int h) {
		g.drawRect(0, 0, w-1, h-1);
		g.drawImage(img, BORDER_OFFSET, this.getMinimumHeight()/2, Graphics.VCENTER | Graphics.LEFT);
		g.drawString(text, img.getWidth()+ BORDER_OFFSET + SPACE, this.getMinimumHeight() - BORDER_OFFSET, Graphics.BASELINE | Graphics.LEFT);
		//TODO MPS add maybe a background-color
	}

}
